package com.kingint.web.admin.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.BasRoom;
import com.kingint.mbg.dto.floor.BasFloorReturn;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import com.kingint.web.admin.base.service.BasRoomService;
import com.kingint.web.sys.service.UmsMemberService;
import com.kingint.web.util.validateUtil.ValidList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasFloorService;
import com.kingint.mbg.bas.entity.BasFloor;

import java.util.Date;
import java.util.List;


/**
 * @描述：楼层表控制类
 * @作者: wxh
 * @日期: 2020-12-21
 */
@RestController
@RequestMapping("/basFloor")
@Api(value = "BasFloorController", description = "楼层表")
@CrossOrigin
public class BasFloorController{

    @Autowired
    public BasFloorService basFloorService;
    @Autowired
    public BasRoomService basRoomService;
    @Autowired
    private UmsMemberService umsMemberService;


    @ApiOperation(value = "添加楼层表")
    @PostMapping("/create")
    // @PreAuthorize("hasAuthority('basFloor:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasFloor basFloor){
        Integer userHotel = umsMemberService.getUserHotel();
        basFloor.setHotelId(userHotel);
        basFloor.setCreateTime(new Date());
        basFloor.setStatus(0);
        Boolean bool=basFloorService.saveOrUpdate(basFloor);
        if(bool){
            return K.ok().data(basFloor.getId());
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "批量添加楼层表")
    @PostMapping("/insertList")
    //  @PreAuthorize("hasAuthority('basFloor:insertList')")
    public Object create(@RequestBody @Validated(Insert.class)  ValidList<BasFloor> basFloor){
        Boolean bool=basFloorService.saveOrUpdateBatch(basFloor);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "更新楼层表")
    @PutMapping("/update")
    //   @PreAuthorize("hasAuthority('basFloor:update')")
    public Object update(@Validated(Update.class) @RequestBody BasFloor basFloor){
        Boolean bool= basFloorService.updateById(basFloor);
        if(bool){
            return K.ok().data(basFloor.getId());
        }else{
            return K.error();
        }
    }


    @ApiOperation(value = "删除楼层表")
    @DeleteMapping("/delete/{id}")
    //  @PreAuthorize("hasAuthority('basFloor:delete')")
    public Object delete(@PathVariable("id") Integer id){
        QueryWrapper<BasRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("floor_id",id);
        List<BasRoom> list = basRoomService.list(queryWrapper);
        if (list.size()>0)
            return K.error().message("请先删除对应房间");
        Boolean bool= basFloorService.removeById(id);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "根据楼层表名称分页获取楼层表列表")
    @GetMapping("/list")
    //  @PreAuthorize("hasAuthority('basFloor:list')")
    public Object getList(BasFloor basFloor,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<BasFloor> page = new Page<>(pageNum, pageSize);
        IPage<BasFloor> info=basFloorService.pagelist(page,basFloor);
        return K.ok().data(info);
    }

    @ApiOperation(value = "根据编号查询楼层表信息")
    @GetMapping("/{id}")
    // @PreAuthorize("hasAuthority('basFloor:get')")
    public Object getItem(@PathVariable("id") Long id){
        BasFloor basFloor=basFloorService.getById(id);
        return K.ok().data(basFloor);
    }
    //获取对应酒店下的楼层以及楼层下的房间信息
    @ApiOperation(value = "获取对应酒店下的楼层以及楼层下的房间信息")
    @GetMapping("/getFloorRoomList")
    public Object getFloorRoomList(){
        List<BasFloorReturn> floorRoomList=basFloorService.getFloorRoomList();
        return K.ok().data(floorRoomList);
    }
    @ApiOperation(value = "根据编号查询楼层表信息")
    @GetMapping("/getfloors")
    // @PreAuthorize("hasAuthority('basFloor:get')")
    public Object getfloors(){
        Integer userHotel = umsMemberService.getUserHotel();
        QueryWrapper<BasFloor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("hotel_id",userHotel);
        List<BasFloor> list = basFloorService.list(queryWrapper);
        return K.ok().data(list);
    }
}